package chatclient.applets;

import chatclient.netio.AttrList;
import chatclient.applets.challengees.Challengee;
import chatclient.applets.challengees.ChallengeeID;
import chatclient.applets.challengees.ChallengeePassword;
import chatclient.netio.Attr;
import chatclient.tools.ChatException;
import chatclient.tools.ChatListener;
import chatclient.netio.ClientAttr;
import chatclient.netio.Command;
import chatclient.netio.Connection;
import chatclient.netio.ConnectionListener;
import chatclient.netio.Event;
import chatclient.tools.Filter;
import chatclient.tools.FilterKey;
import chatclient.tools.Idler;
import chatclient.netio.Message;
import chatclient.netio.Perms;
import java.awt.Color;
import java.io.*;
import java.net.URL;
import java.security.MessageDigest;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;

public class Chat extends JApplet
        implements ConnectionListener {

    public static final String copyrightInfo = "\251 <insert copyright here>";
    public static final String version = "1.01";
    private static boolean stopped;

    public static Chat findChat(String s) {
        return (Chat) chats.get(s);
    }

    private void lockListeners(boolean flag) {
        synchronized (listeners) {
            listenersLocks++;
            if (flag && newListeners == null) {
                newListeners = (Vector) listeners.clone();
            }
        }
    }

    private void unlockListeners() {
        synchronized (listeners) {
            if (--listenersLocks == 0 && newListeners != null) {
                listeners = newListeners;
            }
        }
    }

    public void addChatListener(ChatListener chatlistener, String s) {
        if (s != null) {
            Vector vector = (Vector) histories.get(s);
            if (vector == null) {
                chatlistener.reinitialize();
            } // else if (vector.size() >= maxClientHistory) {
            //     chatlistener.reinitialize();
            // }
            else {
                for (Enumeration enumeration = vector.elements(); enumeration.hasMoreElements();) {
                    Event event = (Event) enumeration.nextElement();
                    try {
                        chatlistener.doEvent(event, false);
                    } catch (Exception exception) {
                        if (debug) {
                            System.err.println(event);
                        }
                        exception.printStackTrace();
                    }
                }

            }
        }
        lockListeners(true);
        if (!newListeners.contains(chatlistener)) {
            if (debug) {
                System.err.println((new StringBuilder("Registering ")).append(chatlistener).append(" for ").append(this).toString());
            }
            newListeners.addElement(chatlistener);
        }
        unlockListeners();
    }

    public void removeChatListener(ChatListener chatlistener, String s) {
        lockListeners(true);
        removeChatListener2(chatlistener, s);
        unlockListeners();
    }

    private void removeChatListener2(ChatListener chatlistener, String s) {
        if (s != null) {
            histories.put(s, new Vector());
        }
        newListeners.removeElement(chatlistener);
        if (debug) {
            System.err.println((new StringBuilder("Unregistering ")).append(chatlistener).append(" for ").append(this).toString());
        }
    }

    private void removeAllChatListeners() {
        lockListeners(true);
        ChatListener chatlistener;
        for (; newListeners.size() > 0; removeChatListener2(chatlistener, chatlistener.unlisten())) {
            chatlistener = (ChatListener) listeners.elementAt(0);
        }

        unlockListeners();
    }

    public Object recallState(String s) {
        return states.remove(s);
    }

    public void saveState(String s, Object obj) {
        states.put(s, obj);
    }

    public String getMyName() {
        return myName;
    }

    public String getMyChannel() {
        return myActiveChannel;
    }

    public AttrList getMyChannelInfo() {
        return getChannelInfo(myActiveChannel);
    }

    public AttrList getChannelInfo(String s) {
        return s != null ? (AttrList) myChannelsInfo.get(s) : null;
    }

    public String getMyChannelInfo(int i) {
        AttrList attrlist = getMyChannelInfo();
        if (attrlist != null) {
            try {
                ClientAttr clientattr = (ClientAttr) attrlist.get(ClientAttr.CHANNELINFO, i);
                return clientattr == null ? null : clientattr.getString();
            } catch (ChatException chatexception) {
                return null;
            }
        } else {
            return null;
        }
    }

    public String getMyUserInfo(int i) {
        if (myUserInfo != null) {
            try {
                ClientAttr clientattr = (ClientAttr) myUserInfo.get(ClientAttr.USERINFO, i);
                return clientattr == null ? null : clientattr.getString();
            } catch (ChatException chatexception) {
                return null;
            }
        } else {
            return null;
        }
    }

    public Vector getHistory() {
        return evtHistory;
    }

    public String getHistoryAsHtml() {
        URL url = getCodeBase();
        try {
            if (getParameter("smilies") != null) {
                url = new URL(url, getParameter("smilies"));
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        StringBuffer stringbuffer = new StringBuffer();
        for (int i = 0; i < evtHistory.size(); i++) {
            Event event = (Event) evtHistory.elementAt(i);
            appendEventAsHtml(stringbuffer, event, url);
            if (event.getId() == Event.LOG) {
                ClientAttr clientattr = null;
                for (Enumeration enumeration = event.getParams().getEnumeration(); enumeration.hasMoreElements();) {
                    ClientAttr clientattr1 = (ClientAttr) enumeration.nextElement();
                    switch (clientattr1.getId()) {
                        default:
                            break;

                        case ClientAttr.LOG_TIME:
                            clientattr = clientattr1;
                            break;

                        case ClientAttr.LOG_EVENT:
                            try {
                                Event event1 = new Event(new ByteArrayInputStream(clientattr1.getValue()));
                                AttrList attrlist = event1.getParams();
                                if (clientattr != null) {
                                    attrlist.set(clientattr);
                                    clientattr = null;
                                }
                                appendEventAsHtml(stringbuffer, event1, url);
                            } catch (Exception exception1) {
                            }
                            break;
                    }
                }

            }
        }

        return stringbuffer.toString();
    }

    private void appendEventAsHtml(StringBuffer stringbuffer, Event event, URL url) {
        AttrList attrlist = event.getParams();
        boolean flag = false;
        String s = null;
        String s1 = null;
        StringBuffer stringbuffer1 = new StringBuffer();
        StringBuffer stringbuffer2 = new StringBuffer();
        stringbuffer.append("<table cellpadding=0 cellspacing=0>").append("\n");
        stringbuffer.append("<tr>");
        try {
            int i = 0;
            boolean flag1 = false;
            for (Enumeration enumeration = attrlist.getEnumeration(); enumeration.hasMoreElements();) {
                ClientAttr clientattr = (ClientAttr) enumeration.nextElement();
                switch (clientattr.getId()) {
                    case ClientAttr.NICKNAME:
                        s1 = clientattr.getString();
                        i++;
                        break;
                }
            }

            if (i > 1) {
                s1 = null;
            }
            if (s1 != null) {
                flag = s1.equalsIgnoreCase(myName);
            }
        } catch (Exception exception) {
            s1 = null;
        }
        if (flag) {
            s = getParameter((new StringBuilder("my")).append(event.getName()).toString().toLowerCase());
        }
        if (s == null) {
            s = getParameter(event.getName().toLowerCase());
        }
        try {
            Message.expand(s, attrlist, new StringBuffer[]{
                        stringbuffer1, stringbuffer2
                    }, 0, getFilters());
            stringbuffer.append("\n").append("<td valign=top><div style=\"white-space: nowrap;\">");
            Message.convertToHtml(stringbuffer, stringbuffer1.toString(), url);
            stringbuffer.append("</div></td>");
            stringbuffer.append("\n").append("<td valign=top>");
            Message.convertToHtml(stringbuffer, stringbuffer2.toString(), url);
            stringbuffer.append("</td>");
        } catch (Exception exception1) {
            exception1.printStackTrace();
        }
        stringbuffer.append("</tr>").append("\n");
        stringbuffer.append("</table>").append("\n");
    }

    public Perms getMyPerms() {
        return myPerms;
    }
    
    public boolean canHide()
    {
        return myPerms.get(Perms.HIDE);
    }

    public String getMyProfile() {
        return myProfile;
    }

    public Perms getMyChannelPerms() {
        return myActiveChannel != null ? (Perms) myChannelsPerms.get(myActiveChannel) : null;
    }

    public String getSelectedNick() {
        return selectedNick;
    }

    @Override
    public void init() {

        try {
            debug = getParameter("debug") != null && Integer.parseInt(getParameter("debug")) > 0;
            if (debug) {
                System.err.println((new StringBuilder("Init started ")).append(this).toString());
            }

            status = Chat.copyrightInfo;
            clrError = Color.red;
            clrCommand = Color.yellow;
            clrEvent = Color.green;
            ignoreColor = false;
            ignorePrivate = false;
            ignorePicture = false;
            textColor = null;
            stopped = false;
            hidden = false;
            blockCommand = false;
            tmpLoginName = "";

            initChat();

            if (debug) {
                System.err.println((new StringBuilder("Init finished ")).append(this).toString());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void destroy() {

        try {
            if (debug) {
                System.err.println((new StringBuilder("Destroy started ")).append(this).toString());
            }

            cleanupChat();

            if (debug) {
                System.err.println((new StringBuilder("Destroy finished ")).append(this).toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void start() {
        try {
            if (debug) {
                System.err.println((new StringBuilder("Start ")).append(this).toString());
            }
            stopped = false;


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void stop() {
        try {
            if (debug) {
                System.err.println((new StringBuilder("Stop ")).append(this).toString());
            }
            stopped = true;
            cleanupChat();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public String getAppletInfo() {
        return Chat.copyrightInfo;
    }

    @Override
    public String[][] getParameterInfo() {
        return (new String[][]{
                    new String[]{
                        "port", "1-65535", "port to connect to"
                    }
                });
    }

    /*
    @Override
    public void paintComponent(Graphics g) {
    if (cxn == null) {
    return;
    }
    g.setColor(getContentPane().getBackground());
    g.setPaintMode();
    //for (; pendingEvents.size() > 0; pendingEvents.removeElementAt(0)) {
    //    doEventInternal((Event) pendingEvents.elementAt(0));
    //}
    
    }
     */
    public boolean mouseEnter(Event event, int i, int j) {
        showStatus(status);
        return true;
    }

    private void initChat() {
        String s;
        useAWTEventQueue = (s = getParameter("useAWTEventQueue")) != null ? Integer.parseInt(s) : 0;
        quotes = (s = getParameter("quotes")) != null ? s : "'\"";
        maxChannels = (s = getParameter("maxChannels")) != null ? Integer.parseInt(s) : 1;
        maxHistory = (s = getParameter("maxHistory")) != null ? Integer.parseInt(s) : 500;
        maxClientHistory = (s = getParameter("maxClientHistory")) != null ? Integer.parseInt(s) : 500;
        //frameDelay = (s = getParameter("frameDelay")) != null ? Long.parseLong(s) : 1500L;
        frameDelay = 700;
        extra = getParameter("extra");
        myName = null;
        pendingEvents = new ConcurrentLinkedQueue<Event>();
        evtHistory = new Vector();
        doPrivateWindows = true;
        ignoreNicks = new Hashtable();
        ignoreNickPatterns = new Vector();
        ignorePrivateNickPatterns = new Vector();
        ignoreEvents = new boolean[53];
        myUserInfo = null;
        myPerms = null;
        myProfile = null;
        myActiveChannel = null;
        myChannels = new Hashtable();
        myChannelsLru = new Vector();
        myChannelsInfo = new Hashtable();
        myChannelsPerms = new Hashtable();
        myChannelsProfile = new Hashtable();
        histories = new Hashtable();
        states = new Hashtable();
        allowedHosts = null;
        listeners = new Vector();
        evtLastParms = new Hashtable();
        nextLogTime = 0;
        chats.put(getParameter("instance"), this);
        for (int i = 0; getParameter((new StringBuilder("client")).append(i).toString()) != null; i++) {
            histories.put(getParameter((new StringBuilder("client")).append(i).toString()), new Vector());
        }

        try {
            if (getParameter("filter") != null) {
                filter = new Filter(new URL(getCodeBase(), getParameter("filter")), true);
            }
        } catch (Exception exception1) {
            exception1.printStackTrace();
        }
        try {
            if (getParameter("smileyFilter") != null) {
                smileyFilter = new Filter(new URL(getCodeBase(), getParameter("smileyFilter")), false);
            }
        } catch (Exception exception2) {
            exception2.printStackTrace();
        }
        actionCmds = new Hashtable();
        try {
            if (getParameter("actionCmds") != null) {
                Vector vector = new Vector();
                parseStrings(vector, '|', getParameter("actionCmds"));
                String s1;
                for (Enumeration enumeration = vector.elements(); enumeration.hasMoreElements(); actionCmds.put(s1, s1)) {
                    s1 = (String) enumeration.nextElement();
                }

            }
        } catch (Exception exception3) {
            exception3.printStackTrace();
        }
        try {
            if (getParameter("welcome") != null) {
                StringBuffer stringbuffer = new StringBuffer();
                AttrList attrlist1 = new AttrList();
                Message.expand(getParameter("welcome"), attrlist1, new StringBuffer[]{
                            stringbuffer
                        }, 0, null);
                attrlist1.add(new ClientAttr(ClientAttr.NOTE_TEXT, stringbuffer.toString()));
                doEvent(new Event(Event.NOTE, attrlist1));
            }
        } catch (Exception exception4) {
            exception4.printStackTrace();
        }
        try {
            final String sHost = getParameter("host");
            final int iPort = Integer.parseInt(getParameter("port"));

            Runnable r = new Runnable() {

                @Override
                public void run() {
                    connect(host, port);
                }
                private final String host;
                private final int port;

                {
                    host = sHost;
                    port = iPort;
                }
            };

            Thread t = new Thread(r);
            t.start();

        } catch (Exception exception5) {
            setStatus(exception5);
        }
    }

    private void cleanupChat() {

        removeAllChatListeners();
        chats.remove(getParameter("name"));
        if (cxn != null) {
            Connection connection = cxn;
            cxn = null;
            connection.destroy();
        }
        if (idler != null) {
            idler.quit();
        }
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(Throwable throwable) {
        showStatus(status = throwable.toString());
        throwable.printStackTrace();
    }

    public void sendCommand(Command command)
            throws Exception {
        
        System.out.println("ADZ - Chat.java->sendCommand::"+command);
        if (debug) {
            System.err.println((new StringBuilder("Sending ")).append(command).toString());
        }

        cxn.sendCommand(command);

        if (idler != null) {
            idler.activity();
        }
    }

    private void sendCommand(byte command, String channelName, String targetName, AttrList attrlist) {
        System.out.println("ADZ - Chat.java->sendCommand::"+command+"::"+channelName+"::"+targetName+"::"+attrlist);
        try {
            if (targetName != null && targetName.length() > 0 && !targetName.equals("*")) {
                attrlist.add(new ClientAttr(ClientAttr.TARGETNAME, targetName));
            }
            if (channelName != null) {
                attrlist.add(new ClientAttr(ClientAttr.CHANNELNAME, channelName));
            }
            sendCommand(new Command(command, attrlist));
        } catch (Exception exception) {
            setStatus(exception);
        }
    }

    private void sendCommand(byte command, String channelName, String targetName) {
        System.out.println("ADZ - Chat.java->sendCommand::"+command+"::"+channelName+"::"+targetName);
        sendCommand(command, channelName, targetName, new AttrList());
    }

    private void sendCommand(byte command, AttrList attrlist) {
        sendCommand(command, null, null, attrlist);
    }

    private void sendCommand(byte command) {
        sendCommand(command, null, null, new AttrList());
    }

    @Override
    public void doEvent(Event event) {
        if (debug) {
            System.err.println((new StringBuilder("Received ")).append(event).toString());
        }
        System.out.println("ADZ - Chat.java->doEvent::"+event);
        doEventInternal(event);
    }

    public String getChallengeePassword()
    {
        ChallengeePassword cpw = new ChallengeePassword();
        return cpw.getPassword();
    }
    
    public void sendChallengeePasswordResponse(String password) {

        try {
            
            byte abyte0[] = MessageDigest.getInstance("MD5").digest((new StringBuilder(String.valueOf(loginPwDigestSalt))).append(password).toString().getBytes("ISO-8859-1"));

            AttrList attrlist = new AttrList();
            attrlist.add(new ClientAttr(ClientAttr.DIGEST, abyte0));
            sendCommand(Command.RESPONSE, attrlist);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 
     * @param event 
     */
    private void doEventInternal(Event event) {
        System.out.println("ADZ - Chat.java->doEventInternal::"+event);
        boolean ignored = false;
        if (debug) {
            System.err.println((new StringBuilder("Handling ")).append(event).toString());
        }
        evtHistory.addElement(event);
        if (evtHistory.size() > maxHistory) {
            evtHistory.removeElementAt(0);
        }
        AttrList attrlist = event.getParams();
        ClientAttr clientattr = (ClientAttr) attrlist.get(ClientAttr.NICKNAME);
        String nickName = null;
        ClientAttr clientattr1 = (ClientAttr) attrlist.get(ClientAttr.CHANNELNAME);
        String channelName = null;
        try {
            nickName = clientattr != null ? clientattr.getString() : null;
        } catch (ChatException chatexception) {
        }
        try {
            channelName = clientattr1 != null ? clientattr1.getString() : null;
        } catch (ChatException chatexception1) {
        }
        String lcChannelName = channelName != null ? channelName.toLowerCase() : null;
        boolean isMyNickName = myName != null && nickName != null && nickName.equalsIgnoreCase(myName);
        boolean isMyActiveChannel = myActiveChannel != null && channelName != null && channelName.equals(myActiveChannel);
        boolean isMyChannel = channelName != null && myChannels.get(lcChannelName) != null;
        try {
            switch (event.getId()) {
                case Event.ADDEDCHATTER:
                    System.out.println("ADZ - Chat.java->doEventInternal::EVENT.ADDEDCHATTER::ID="+event.getId());
                    if (myName == null) {
                        myName = nickName;
                        try {
                            setInfo(null, myName, 0, loginInfo);
                            String channel = getParameter("channel");
                            if (channel != null) {
                                if (channel.equals("")) {
                                    sendCommand(Command.JOIN);
                                } else {
                                    sendCommand(Command.JOIN, channel, null);
                                    expectFinalPart = false;
                                }
                            }
                            AttrList attrlist3 = new AttrList();
                            attrlist3.add(new ClientAttr(ClientAttr.VARNAME, "allowedhosts"));
                            sendCommand(Command.GETVAR, attrlist3);
                            sendCommand(Command.GETPERMS, null, myName);
                            sendCommand(Command.GETINFO, null, myName);
                        } catch (Exception exception) {
                            setStatus(exception);
                        }
                    }
                    break;
                case Event.CHANGEDNICKNAME:
                    System.out.println("ADZ - Chat.java->doEventInternal::EVENT.CHANGEDNICKNAME::ID="+event.getId());
                    if (isMyNickName) {
                        ClientAttr ctn = (ClientAttr) attrlist.get(ClientAttr.TARGETNAME);
                        String tn = null;
                        try {
                            tn = ctn != null ? ctn.getString() : null;
                        } catch (ChatException chatexception) {
                        }
                        if (tn != null) {
                            myName = tn;
                        }
                    }
                    break;

                case Event.CHALLENGE:
                    System.out.println("ADZ - Chat.java->doEventInternal::EVENT.CHALLENGE::ID="+event.getId());
                    try {
                        String challengeClassName = attrlist.get(ClientAttr.CHALLENGE).getString();
                        String digestSalt = attrlist.get(ClientAttr.DIGEST_SALT).getString();


                        if (challengeClassName.toLowerCase().contains("password")) {
                            Attr wpwAttr = attrlist.get(ClientAttr.INDEX);
                            boolean wrongPw = false;
                            if (wpwAttr != null && wpwAttr.getInt() == 1) {
                                wrongPw = true;
                            }

                            loginPwWasWrong = wrongPw;
                            loginPwDigestSalt = digestSalt;

                            doInternalEvent("passwordlogin");

                        } else {
                            Challengee challengee = new ChallengeeID();
                            byte abyte0[];
                            abyte0 = challengee.getResponse(this, digestSalt);
                            AttrList attrlist4 = new AttrList();
                            attrlist4.add(new ClientAttr(ClientAttr.DIGEST, abyte0));
                            sendCommand(Command.RESPONSE, attrlist4);
                        }


                    } catch (Exception exception2) {
                        exception2.printStackTrace();
                        break;
                    }

                    break;

                case Event.VARIABLE:
                    System.out.println("ADZ - Chat.java->doEventInternal::EVENT.VARIABLE::ID="+event.getId());
                    if (attrlist.get(ClientAttr.VARNAME).getString().equalsIgnoreCase("allowedhosts")) {
                        Vector vector = allowedHosts;
                        allowedHosts = new Vector();
                        for (Enumeration enumeration = attrlist.getEnumeration(); enumeration.hasMoreElements();) {
                            ClientAttr clientattr2 = (ClientAttr) enumeration.nextElement();
                            if (clientattr2.getId() == ClientAttr.VARVALUE) {
                                allowedHosts.addElement(clientattr2.getString());
                            }
                        }

                        if (vector == null) {
                            doInternalEvent("login");
                        }
                    }
                    break;

                case Event.ADDIGNORE:
                    System.out.println("ADZ - Chat.java->doEventInternal::EVENT.ADDIGNORE::ID="+event.getId());
                    String ignore = attrlist.get(ClientAttr.TARGETNAME).getString();
                    ignoreNicks.put(ignore.toLowerCase(), ignore);
                    sendUpdated();

                    AttrList attrs = new AttrList();
                    attrs.add(new ClientAttr(ClientAttr.NICKNAME, ignore));
                    doEvent(new Event(Event.CHANGEDIGNORE, attrs));
                    break;

                case Event.OPENPRIVATE:
                    System.out.println("ADZ - Chat.java->doEventInternal::EVENT.OPENPRIVATE::ID="+event.getId());
                    String pname = attrlist.get(ClientAttr.TARGETNAME).getString();
                    realStartPrivate(pname);
                    break;

                case Event.JOINED:
                    System.out.println("ADZ - Chat.java->doEventInternal::EVENT.JOINED::ID="+event.getId());
                case Event.PARTED:
                    System.out.println("ADZ - Chat.java->doEventInternal::EVENT.PARTED::ID="+event.getId());
                    if (isMyNickName) {
                        if (event.getId() == Event.JOINED) {
                            expectJoin = false;
                            partLruChannels(0);
                            myActiveChannel = channelName;
                            myChannels.put(lcChannelName, channelName);
                            myChannelsLru.addElement(channelName);
                            myChannelsInfo.put(channelName, new AttrList());
                            myChannelsPerms.put(channelName, Perms.fromAttr(attrlist.get(ClientAttr.PERMS)));
                            myChannelsProfile.put(channelName, attrlist.get(ClientAttr.PROFILE).getString());
                            changedActiveChannel();
                            sendCommand(Command.NICKLIST, channelName, null);
                            if (createChannelName != null && createChannelName.equalsIgnoreCase(channelName) && createChannelInfo != null) {
                                setInfo(channelName, null, 0, createChannelInfo);
                                createChannelName = null;
                                createChannelInfo = null;
                            } else {
                                sendCommand(Command.GETINFO, channelName, null);
                            }
                        } else {
                            myActiveChannel = null;
                            myChannels.remove(lcChannelName);
                            myChannelsLru.removeElement(channelName);
                            myChannelsInfo.remove(channelName);
                            myChannelsPerms.remove(channelName);
                            myChannelsProfile.remove(channelName);
                            if (isMyActiveChannel && !expectJoin) {
                                activateMruChannel();
                            }
                        }
                    }
                    if (channelName != null) {
                        AttrList attrlist1 = (AttrList) myChannelsInfo.get(channelName);
                        if (attrlist1 != null) {
                            attrlist1.set(attrlist.get(ClientAttr.MEMBERCOUNT));
                            attrlist1.set(attrlist.get(ClientAttr.CHATTERCOUNT));
                        }
                    }
                    ignored = isIgnoreNick(nickName);
                    break;

                case Event.CHANNELINFO:
                    System.out.println("ADZ - Chat.java->doEventInternal::EVENT.CHANNELINFO::ID="+event.getId());
                    if (isMyChannel) {
                        myChannelsInfo.put(channelName, attrlist);
                    }
                    break;

                case Event.NOTE:
                    System.out.println("ADZ - Chat.java->doEventInternal::EVENT.NOTE::ID="+event.getId());
                    if (nickName != null) {
                        ignored = isIgnoreNick(nickName) || channelName == null && attrlist.get(ClientAttr.TARGETNAME) != null && isIgnorePrivateNick(nickName);
                    }
                    break;

                case Event.PERMS:
                    System.out.println("ADZ - Chat.java->doEventInternal::EVENT.PERMS::ID="+event.getId());
                    if (isMyNickName) {
                        if (channelName == null) {
                            myPerms = Perms.fromAttr(attrlist.get(ClientAttr.PERMS));
                        }
                        if (isMyChannel) {
                            myChannelsPerms.put(channelName, Perms.fromAttr(attrlist.get(ClientAttr.PERMS)));
                        }
                    }
                    break;

                case Event.SETPROFILE:
                    System.out.println("ADZ - Chat.java->doEventInternal::EVENT.SETPROFILE::ID="+event.getId());
                    if (isMyNickName) {
                        if (channelName == null) {
                            myProfile = attrlist.get(ClientAttr.PROFILE).getString();
                        }
                        if (isMyChannel) {
                            myChannelsProfile.put(channelName, attrlist.get(ClientAttr.PROFILE).getString());
                        }
                    }
                    break;

                case Event.USERINFO:
                    System.out.println("ADZ - Chat.java->doEventInternal::EVENT.USERINFO::ID="+event.getId());
                    if (isMyNickName) {
                        myUserInfo = attrlist;
                    }
                    ignored = isIgnoreNick(nickName);
                    break;
                case Event.COLORUSE:
                    System.out.println("ADZ - Chat.java->doEventInternal::EVENT.COLORUSE::ID="+event.getId()+":Attrlist="+event.getParams());
                    AttrList at = event.getParams();
                    boolean isGuest = Boolean.parseBoolean(at.get(ClientAttr.VARVALUE).getAsString());
                    boolean preventGuestColorUse = Boolean.parseBoolean(getParameter("preventguestcoloruse"));
                    if (isGuest && preventGuestColorUse)
                    {
                        try {
                            StringBuffer stringbuffer = new StringBuffer();
                            AttrList atl = new AttrList();
                            Message.expand(getParameter("guestpreventcolours"), atl, new StringBuffer[]{
                                stringbuffer
                            }, 0, null);
                            atl.add(new ClientAttr(ClientAttr.NOTE_TEXT, stringbuffer.toString()));
                            atl.add(new ClientAttr(ClientAttr.CHANNELNAME, this.getMyChannel()));
                            doEvent(new Event(Event.NOTE, atl));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    else
                    {
                        setTextColor(at.get(ClientAttr.TEXTCOLOR).getAsString());
                    }
                    break;
            }
            if (event.getId() == Event.LOG) {
                AttrList attrlist2 = new AttrList();
                for (Enumeration enumeration1 = attrlist.getEnumeration(); enumeration1.hasMoreElements();) {
                    ClientAttr clientattr3 = (ClientAttr) enumeration1.nextElement();
                    switch (clientattr3.getId()) {
                        case ClientAttr.LOG_EVENT:
                            System.out.println("ADZ - Chat.java->doEventInternal::ClientAttr.LOG_EVENT::ID="+event.getId());
                            Event event1 = new Event(new ByteArrayInputStream(clientattr3.getValue()));
                            fixEventParams(event1.getParams());
                            attrlist2.add(new ClientAttr(ClientAttr.LOG_EVENT, event1.getByteCache(false)));
                            break;

                        case ClientAttr.LOG_TIME:
                            System.out.println("ADZ - Chat.java->doEventInternal::ClientAttr.LOG_TIME::ID="+event.getId());
                            nextLogTime = Math.max(nextLogTime, clientattr3.getInt() + 1);

                        default:
                            System.out.println("ADZ - Chat.java->doEventInternal::ClientAttr.[default]::ID="+event.getId());
                            attrlist2.add(clientattr3);
                            break;
                    }
                }

                attrlist = attrlist2;
                event = new Event(event.getId(), attrlist);
            } else {
                fixEventParams(attrlist);
            }
            if (nickName != null && myName != null && !isMyNickName) {
                ignored |= event.getId() < ignoreEvents.length && ignoreEvents[event.getId()];
            }
        } catch (Exception exception1) {
            setStatus(exception1);
        }
        if (debug) {
            System.err.println((new StringBuilder("Modified ")).append(event).toString());
        }
        evtLastParms.put(event.getName().toLowerCase(), event.getParams());
        onevent(event.getName().toLowerCase(), event.getParams());
        synchronized (histories) {
            for (Enumeration enumeration2 = histories.elements(); enumeration2.hasMoreElements();) {
                Vector vector1 = (Vector) enumeration2.nextElement();
                vector1.addElement(event);
                if (vector1.size() > maxClientHistory) {
                    vector1.remove(1);
                }
            }

        }
        lockListeners(false);
        for (Enumeration enumeration3 = listeners.elements(); enumeration3.hasMoreElements();) {
            try {
                ((ChatListener) enumeration3.nextElement()).doEvent(event, ignored);
            } catch (Exception exception3) {
                if (debug) {
                    System.err.println(event);
                }
                exception3.printStackTrace();
            }
        }

        unlockListeners();
        //getContentPane().setBackground(clrEvent);
        //getContentPane().repaint();
    }

    public void doInternalEvent(String s) {
        doInternalEvent(s, new AttrList());
    }

    public void doInternalEvent(String s, AttrList attrlist) {
        if (debug) {
            System.err.println((new StringBuilder("Internal event ")).append(s).append(" ").append(attrlist).toString());
        }
        onevent(s, attrlist);
        lockListeners(false);
        for (Enumeration enumeration = listeners.elements(); enumeration.hasMoreElements(); ((ChatListener) enumeration.nextElement()).doInternalEvent(s, attrlist)) {
        }
        unlockListeners();
    }

    @Override
    public void connectionClosed() {

        doInternalEvent("disconnect");
        cleanupChat();
    }

    public Filter[] getFilters() {
        return ignorePicture ? (new Filter[]{
                    filter
                }) : (new Filter[]{
                    smileyFilter, filter
                });
    }

    public void changedActiveChannel() {
        lockListeners(false);
        for (Enumeration enumeration = listeners.elements(); enumeration.hasMoreElements(); ((ChatListener) enumeration.nextElement()).changedActiveChannel()) {
        }
        unlockListeners();
    }

    public void clearNotes(String s) {
        lockListeners(false);
        for (Enumeration enumeration = listeners.elements(); enumeration.hasMoreElements(); ((ChatListener) enumeration.nextElement()).clearedNotes(s)) {
        }
        unlockListeners();
    }

    public void selectNick(String nickname, String s1, int i) {
        selectedNick = nickname;
        lockListeners(false);
        for (Enumeration enumeration = listeners.elements(); enumeration.hasMoreElements(); ((ChatListener) enumeration.nextElement()).selectedNick(nickname)) {
        }
        unlockListeners();
        if (i > 0) {
            try {
                sendCommand(Command.GETINFO, null, nickname);
            } catch (Exception exception) {
                setStatus(exception);
            }
        }
    }

    public void deselectNick() {
        selectedNick = null;
        lockListeners(false);
        for (Enumeration enumeration = listeners.elements(); enumeration.hasMoreElements(); ((ChatListener) enumeration.nextElement()).deselectedNick()) {
        }
        unlockListeners();
    }

    public void startPrivate(String s) {
        try {
            AttrList attrlist = new AttrList();
            attrlist.add(new ClientAttr(ClientAttr.TARGETNAME, s));
            sendCommand(Command.REQUESTPRIVATE, null, null, attrlist);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void realStartPrivate(String s) {
        lockListeners(false);
        for (Enumeration enumeration = listeners.elements(); enumeration.hasMoreElements(); ((ChatListener) enumeration.nextElement()).startedPrivate(s)) {
        }
        unlockListeners();
    }

    public void endPrivate(String s) {
        lockListeners(false);
        for (Enumeration enumeration = listeners.elements(); enumeration.hasMoreElements(); ((ChatListener) enumeration.nextElement()).endedPrivate(s)) {
        }
        unlockListeners();
    }

    public void idle(int i, int j, int k) {
        try {
            if (k > 0) {
                AttrList attrlist = new AttrList();
                attrlist.add(new ClientAttr(ClientAttr.INDEX, Integer.toString(i + 1)));
                attrlist.add(new ClientAttr(ClientAttr.IDLETIME, j));
                doInternalEvent("idle", attrlist);
            } else {
                sendCommand(Command.QUIT);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public boolean connect(String server, int port) {
        if (cxn != null) {
            cleanupChat();
        }

        while (cxn == null) {
            try {
                doInternalEvent("connecting");
                cxn = new Connection(server == null ? getCodeBase().getHost() : server, port, this);
            } catch (Exception e) {
                cxn = null;
                System.out.println("Cannot connect to server: " + e.getMessage());
                doInternalEvent("noconnect");
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException ex) {
                }
            }
        }

        try {
            doInternalEvent("connect");
        } catch (Exception exception) {
            setStatus(exception);
            doInternalEvent("error");
        }
        if (cxn != null) {
            try {
                Vector vector = new Vector();
                parseStrings(vector, ',', getParameter("idletime"));
                if (vector.size() > 0) {
                    for (int j = 0; j < vector.size(); j++) {
                        vector.setElementAt(new Integer((String) vector.elementAt(j)), j);
                    }

                    idler = new Idler(this, vector);
                }
            } catch (Exception exception1) {
                exception1.printStackTrace();
            }
        }
        return true;
    }

    public boolean login(String nick, String loginInfo, String password)
            throws Exception {

        
        if (getParameter("nick") != null) {
            nick = getParameter("nick");
        }
        if (getParameter("loginInfo") != null) {
            loginInfo = getParameter("loginInfo").replace('|', '\037');
        }
        if (getParameter("password") != null) {
            password = getParameter("password");
        }
        this.loginInfo = loginInfo != null ? loginInfo : "";


        AttrList attrlist = new AttrList();
        if (nick != null && nick.length() > 0) {
            attrlist.add(new ClientAttr(ClientAttr.NICKNAME, nick));
            tmpLoginName = nick;
        }
        if (password != null && password.length() > 0) {
            attrlist.add(new ClientAttr(ClientAttr.PASSWORD, password));
        }
        String s3;
        if ((s3 = getParameter("ident")) != null && s3.length() > 0) {
            attrlist.add(new ClientAttr(ClientAttr.IDENT, s3));
        }
        if ((s3 = getParameter("profile")) != null && s3.length() > 0) {
            attrlist.add(new ClientAttr(ClientAttr.PROFILE, s3));
        }
        if ((s3 = getParameter("digest")) != null && s3.length() > 0) {
            attrlist.add(new ClientAttr(ClientAttr.DIGEST, bytesFromHexString(s3)));
        }
        if ((s3 = getParameter("digestTime")) != null && s3.length() > 0) {
            attrlist.add(new ClientAttr(ClientAttr.DIGEST_TIME, Integer.parseInt(s3)));
        }
        if ((s3 = getParameter("digestSalt")) != null && s3.length() > 0) {
            attrlist.add(new ClientAttr(ClientAttr.DIGEST_SALT, s3));
        }

        sendCommand(Command.SIGNUP, attrlist);
        return true;
    }

    private static byte[] bytesFromHexString(String s) {
        if (s == null) {
            return null;
        }
        byte abyte0[] = new byte[s.length() / 2];
        for (int i = 0; i < s.length(); i += 2) {
            abyte0[i / 2] = (byte) Integer.parseInt(s.substring(i, i + 2), 16);
        }

        return abyte0;
    }

    private void parseStrings(Vector vector, char c, String strings)
            throws Exception {
        int i = 0;
        if (strings == null) {
            return;
        }
        int j;
        for (; i < strings.length(); i = j + 1) {
            j = strings.indexOf(c, i);
            if (j == -1) {
                j = strings.length();
            }
            vector.addElement(strings.substring(i, j));
        }

    }

    private void parseStrings(AttrList attrlist, byte clientAttrId, char c, String strings)
            throws Exception {
        int i = 0;
        if (strings == null) {
            return;
        }
        int j;
        for (; i < strings.length(); i = j + 1) {
            j = strings.indexOf(c, i);
            if (j == -1) {
                j = strings.length();
            }
            attrlist.add(new ClientAttr(clientAttrId, strings.substring(i, j)));
        }

    }

    public boolean setInfo(String channelName, String targetName, int index, String infoStrings)
            throws Exception {
        AttrList attrlist = new AttrList();
        byte infoType = ClientAttr.USERINFO;
        if (channelName != null) {
            if (targetName != null) {
                infoType = ClientAttr.MEMBERINFO;
            } else {
                infoType = ClientAttr.CHANNELINFO;
            }
        } else if (targetName != null) {
            infoType = ClientAttr.USERINFO;
        } else {
            infoType = ClientAttr.CHATBOXINFO;
        }
        if (index != 0) {
            attrlist.add(new ClientAttr(ClientAttr.INDEX, Integer.toString(index)));
        }
        parseStrings(attrlist, infoType, '\037', infoStrings);
        sendCommand(Command.SETINFO, channelName, targetName, attrlist);
        return true;
    }

    public void search(String channelName, String nickname, String userinfo, String profile)
            throws Exception {
        AttrList attrlist = new AttrList();
        parseStrings(attrlist, ClientAttr.NICKNAME, '\037', nickname);
        parseStrings(attrlist, ClientAttr.USERINFO, '\037', userinfo);
        parseStrings(attrlist, ClientAttr.PROFILE, '\037', profile);
        sendCommand(Command.FINDNICKS, channelName, null, attrlist);
    }

    private void sendUpdated() {
        lockListeners(false);
        for (Enumeration enumeration = listeners.elements(); enumeration.hasMoreElements(); ((ChatListener) enumeration.nextElement()).updated()) {
        }
        unlockListeners();
    }

    public void sendUpdatedNick(String s) {
        lockListeners(false);
        for (Enumeration enumeration = listeners.elements(); enumeration.hasMoreElements(); ((ChatListener) enumeration.nextElement()).updatedNick(s)) {
        }
        unlockListeners();
    }

    public void sendUpdatedMembers() {
        lockListeners(false);
        for (Enumeration enumeration = listeners.elements(); enumeration.hasMoreElements(); ((ChatListener) enumeration.nextElement()).updatedMembers()) {
        }
        unlockListeners();
    }

    public boolean isIgnoreEvent(String s) {
        return isIgnoreEvent(Event.getId(s));
    }

    private boolean isIgnoreEvent(byte byte0) {
        return byte0 >= 0 && byte0 < ignoreEvents.length && ignoreEvents[byte0];
    }

    public void setIgnoreEvents(String s, boolean flag) {
        int j;
        for (int i = 0; i < s.length(); i = j + 1) {
            j = s.indexOf('|', i);
            if (j == -1) {
                j = s.length();
            }
            byte byte0 = Event.getId(s.substring(i, j));
            if (byte0 >= 0 && byte0 < ignoreEvents.length) {
                ignoreEvents[byte0] = flag;
            }
        }

    }

    public boolean getIgnoreColor() {
        return ignoreColor;
    }

    public void setIgnoreColor(boolean flag) {
        ignoreColor = flag;
    }

    public boolean getIgnorePrivate() {
        return ignorePrivate;
    }

    public void setIgnorePrivate(boolean flag) {
        ignorePrivate = flag;
    }

    public void setHidden(boolean flag) {
        try {
            AttrList attrlist = new AttrList();
            if (flag && !hidden) {
                attrlist.add(new ClientAttr(ClientAttr.PROFILE, "hide"));
                sendCommand(Command.SETPROFILE, null, myName, attrlist);
                hidden = true;
            } else if (!flag && hidden) {
                attrlist.add(new ClientAttr(ClientAttr.PROFILE, "unhide"));
                sendCommand(Command.SETPROFILE, null, myName, attrlist);
                hidden = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean getHidden() {
        return hidden;
    }

    public boolean getIgnorePicture() {
        return ignorePicture;
    }

    public void setIgnorePicture(boolean flag) {
        ignorePicture = flag;
    }

    public boolean isIgnoreNick(String s) {
        if (s == null) {
            return false;
        }
        String s1 = s.toLowerCase();
        if (ignoreNicks.get(s1) != null) {
            return true;
        }
        if (myName != null && myName.equalsIgnoreCase(s)) {
            return false;
        } else {
            return matchIgnoreNickPatternAny(ignoreNickPatterns, s);
        }
    }

    public boolean isIgnorePrivateNick(String s) {
        if (s == null) {
            return false;
        }
        if (myName != null && myName.equalsIgnoreCase(s)) {
            return false;
        }
        if (ignorePrivate) {
            return true;
        } else {
            return matchIgnoreNickPatternAny(ignorePrivateNickPatterns, s);
        }
    }

    public void setIgnoreNick(String s, boolean flag) {
        if (s == null) {
            return;
        }
        if (flag) {
            try {
                AttrList attrlist = new AttrList();
                attrlist.add(new ClientAttr(ClientAttr.TARGETNAME, s));
                sendCommand(Command.REQUESTIGNORE, null, null, attrlist);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            ignoreNicks.remove(s.toLowerCase());
            sendUpdated();

            try {
                AttrList attrlist = new AttrList();
                attrlist.add(new ClientAttr(ClientAttr.NICKNAME, s));
                attrlist.add(new ClientAttr(ClientAttr.EXEC, "1"));
                doEvent(new Event(Event.CHANGEDIGNORE, attrlist));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private boolean matchIgnoreNickPatternAny(Vector vector, String s) {
        String s1 = s.toLowerCase();
        for (Enumeration enumeration = vector.elements(); enumeration.hasMoreElements();) {
            String s2 = (String) enumeration.nextElement();
            if (s2.length() >= 3 && s2.substring(s2.length() - 3).equals("...")) {
                if (s1.startsWith(s2.substring(0, s2.length() - 3).toLowerCase())) {
                    return true;
                }
            } else if (s1.equals(s2.toLowerCase())) {
                return true;
            }
        }

        return false;
    }

    private boolean isIgnoreNickPatternAny(Vector vector, String s) {
        if (s == null) {
            return false;
        }
        for (Enumeration enumeration = vector.elements(); enumeration.hasMoreElements();) {
            if (((String) enumeration.nextElement()).equalsIgnoreCase(s)) {
                return true;
            }
        }

        return false;
    }

    private void setIgnoreNickPatternAny(Vector vector, String s, boolean flag) {
        if (s == null) {
            return;
        }
        if (!isIgnoreNickPattern(s)) {
            if (flag) {
                vector.addElement(s);
                sendUpdated();
            }
        } else if (!flag) {
            vector.removeElement(s);
            sendUpdated();
        }
    }

    public boolean isIgnoreNickPattern(String s) {
        return isIgnoreNickPatternAny(ignoreNickPatterns, s);
    }

    public void setIgnoreNickPattern(String s, boolean flag) {
        setIgnoreNickPatternAny(ignoreNickPatterns, s, flag);
    }

    public boolean isIgnorePrivateNickPattern(String s) {
        return isIgnoreNickPatternAny(ignorePrivateNickPatterns, s);
    }

    public void setIgnorePrivateNickPattern(String s, boolean flag) {
        setIgnoreNickPatternAny(ignorePrivateNickPatterns, s, flag);
    }

    public String getTextColor() {
        return textColor;
    }

    public void setTextColor(String s) {
        textColor = validateColour(s);
    }

    public boolean getPrivateWindows() {
        return doPrivateWindows;
    }

    public void setPrivateWindows(boolean flag) {
        doPrivateWindows = flag;
    }

    private boolean isDelimiter(Character character, char c) {
        return character != null ? character.charValue() == c : Character.isWhitespace(c);
    }

    /**
     * Validates a colour depending on the its RGB composition if the dark colours are not allowed.
     * If the green composition is bigger than the green limit, the colour is considered bright enough and it is accepted.
     * Else, if the blue composition exceeds the blue limit or the red composition exceeds the red limit, the colour is
     * also accepted. Otherwise it is considered a dark color that is not allowed on the chat.
     * @param strColor
     * @return 
     */
    public String validateColour(String strColor)
    {
        if (strColor == null)
            return strColor;
        if (getParameter("allowdarkcolours").equalsIgnoreCase("false"))
        {
            // Get RGB composition of the given text color.
            // Example: For #FF00AA, red=0xFF, green=0x00, blue=0xAA
            int redComposition = Integer.parseInt(strColor.substring(1, 3), 16);
            int greenComposition = Integer.parseInt(strColor.substring(3, 5), 16);
            int blueComposition = Integer.parseInt(strColor.substring(5, 7), 16);
            
            int redColorLimit = Integer.parseInt(getParameter("redColorLimit"), 16);
            int greenColorLimit = Integer.parseInt(getParameter("greenColorLimit"), 16);
            int blueColorLimit = Integer.parseInt(getParameter("blueColorLimit"), 16);
            
            if (greenComposition >= greenColorLimit)
                return strColor;
            else
            {
                if (redComposition >= redColorLimit || blueComposition >= blueColorLimit)
                    return strColor;
                else
                {
                    try {
                        StringBuffer stringbuffer = new StringBuffer();
                        AttrList atl = new AttrList();
                        Message.expand(getParameter("darkcolornotallowed"), atl, new StringBuffer[]{
                            stringbuffer
                        }, 0, null);
                        atl.add(new ClientAttr(ClientAttr.NOTE_TEXT, stringbuffer.toString()));
                        atl.add(new ClientAttr(ClientAttr.CHANNELNAME, this.getMyChannel()));
                        doEvent(new Event(Event.NOTE, atl));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    
                    return this.textColor; // We keep the current color
                }
            }
        }
        else
            return strColor;
    }
    
    /**
     * Called when the text color have to be changed, either with the '/color' command or 
     * through the options in the chat client interface.
     * @param color The required color
     */
    public void sendTextColorCommand(String color)
    {
        //System.out.println("ADZ - Chat.java->sendTextColorCommand:color=" + color);
        
        AttrList at = new AttrList();
        try {
            at.add(new ClientAttr(ClientAttr.TEXTCOLOR, color));
            sendCommand(new Command(Command.COLORUSE, at));
        } catch (ChatException ex) {
            Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Chat.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public boolean input(String channel, String text, boolean noChannel, String targetName)
            throws Exception {
        System.out.println("ADZ - Chat.java->input:text="+text);
        if (text == null || text.length() == 0) {
            return true;
        }
        if (channel == null) {
            channel = myActiveChannel != null ? myActiveChannel : "";
        }
        try {
            AttrList attrlist = new AttrList();
            if ("./".indexOf(text.charAt(0)) < 0) {
                sendText(targetName, noChannel ? null : channel, text, textColor, attrlist);
            } else {
                try {
                    int i = 1;
                    boolean flag1 = false;
                    Vector vector = new Vector();
                    Vector vector1 = new Vector();
                    if (debug) {
                        System.err.println((new StringBuilder("Command: ")).append(text).toString());
                    }
                    while (i < text.length()) {
                        StringBuilder stringbuffer = new StringBuilder();
                        Character character = null;
                        int k = i;
                        char c = '\0';
                        if (!flag1 && quotes.indexOf(c = text.charAt(k)) >= 0) {
                            character = new Character(c);
                            k++;
                        }
                        char c1;
                        for (; k < text.length() && !isDelimiter(character, c1 = text.charAt(k)); k++) {
                            char c3;
                            if (c1 == '\\' && k + 1 < text.length() && ((c3 = text.charAt(k + 1)) == '\\' || isDelimiter(character, c3))) {
                                c1 = c3;
                                k++;
                            }
                            stringbuffer.append(c1);
                        }

                        if (k >= text.length() && character != null) {
                            flag1 = true;
                        } else {
                            vector.addElement(stringbuffer.toString());
                            char c2;
                            while (++k < text.length() && Character.isWhitespace(c2 = text.charAt(k))) {
                            }
                            vector1.addElement(k < text.length() ? ((Object) (text.substring(i).trim())) : ((Object) (stringbuffer.toString())));
                            i = k;
                            flag1 = false;
                        }
                    }
                    String inputCommand = ((String) vector.elementAt(0)).toLowerCase();
                    if (inputCommand.equalsIgnoreCase(lastCommand))
                    {
                        commandReuseCount = (commandReuseCount < Integer.parseInt(getParameter("commandMaxReuse"))) ? commandReuseCount + 1 : commandReuseCount;
                    }
                    else
                    {
                        lastCommand = inputCommand;
                        commandReuseCount = 0;
                    }
                        
                    blockCommand = commandReuseCount == Integer.parseInt(getParameter("commandMaxReuse")) ? true : false;
                    if (blockCommand)
                    {
                        try {
                            StringBuffer stringbuffer = new StringBuffer();
                            AttrList atl = new AttrList();
                            String message = getParameter("blockcommandreuse");
                            message = message.replace("[X]", getParameter("commandMaxReuse"));
                            Message.expand(message, atl, new StringBuffer[]{
                                stringbuffer
                            }, 0, null);
                            atl.add(new ClientAttr(ClientAttr.NOTE_TEXT, stringbuffer.toString()));
                            atl.add(new ClientAttr(ClientAttr.CHANNELNAME, this.getMyChannel()));
                            doEvent(new Event(Event.NOTE, atl));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    else if (vector.size() >= 1) {
                        //String inputCommand = ((String) vector.elementAt(0)).toLowerCase();
                        vector.removeElementAt(0);
                        vector1.removeElementAt(0);
                        if (inputCommand.equals("join") && vector.size() >= 1) {
                            joinChannel((String) vector.elementAt(0), vector.size() >= 2 ? (String) vector1.elementAt(1) : null);
                        } else if (inputCommand.equals("part")) {
                            sendCommand(Command.PART, vector.size() >= 1 ? (String) vector1.elementAt(0) : channel, null);
                        } else if (inputCommand.equals("") && vector.size() >= 1) {
                            sendText(targetName, noChannel ? null : channel, (String) vector1.elementAt(0), textColor, attrlist);
                        } else if (inputCommand.equals("me") && vector.size() >= 1) {
                            attrlist.add(new ClientAttr(ClientAttr.NOTE_ME, ""));
                            sendText(targetName, noChannel ? null : channel, (String) vector1.elementAt(0), textColor, attrlist);
                        } else if (actionCmds.get(inputCommand) != null) {
                            attrlist.add(new ClientAttr(ClientAttr.NOTE_ME, inputCommand));
                            sendText(targetName, noChannel ? null : channel, vector.size() >= 1 ? (String) vector1.elementAt(0) : null, textColor, attrlist);
                        } else if (inputCommand.equals("msg") && vector.size() >= 2) {
                            sendText((String) vector.elementAt(0), null, (String) vector1.elementAt(1), textColor, attrlist);
                        } else if (inputCommand.equals("to") && vector.size() >= 2) {
                            sendText((String) vector.elementAt(0), channel, (String) vector1.elementAt(1), textColor, attrlist);
                        } else if (inputCommand.equals("wall") && vector.size() >= 1) {
                            sendText(null, null, (String) vector1.elementAt(0), textColor, attrlist);
                        } else if (inputCommand.equals("who")) {
                            sendCommand(Command.NICKLIST, vector.size() >= 1 ? (String) vector1.elementAt(0) : channel, null);
                        } else if (inputCommand.equals("users")) {
                            sendCommand(Command.NICKLIST);
                        } else if (inputCommand.equals("whois") && vector.size() >= 1) {
                            sendCommand(Command.GETINFO, null, (String) vector1.elementAt(0));
                        } else if (inputCommand.equals("find") && vector.size() >= 1) {
                            attrlist.add(new ClientAttr(ClientAttr.NICKNAME, (String) vector1.elementAt(0)));
                            sendCommand(Command.FINDNICKS, null, null, attrlist);
                        } else if (inputCommand.equals("findall") && vector.size() >= 1) {
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, (String) vector1.elementAt(0)));
                            sendCommand(Command.FINDNICKS, null, null, attrlist);
                        } else if (inputCommand.startsWith("find") && Character.isDigit(inputCommand.charAt(4))) {
                            int j = Integer.parseInt(inputCommand.substring(4));
                            for (int l = 0; l < j; l++) {
                                attrlist.add(new ClientAttr(ClientAttr.USERINFO, ""));
                            }

                            attrlist.add(new ClientAttr(ClientAttr.USERINFO, (String) vector1.elementAt(0)));
                            sendCommand(Command.FINDNICKS, null, null, attrlist);
                        } else if (inputCommand.equals("kill") && vector.size() >= 1) {
                            sendCommand(Command.KILLCHANNEL, (String) vector1.elementAt(0), null);
                        } else if (inputCommand.equals("kick") && vector.size() >= 1) {
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, "banned"));
                            sendCommand(Command.SETPROFILE, channel, (String) vector.elementAt(0), attrlist);
                        } else if (inputCommand.equals("skick") && vector.size() >= 1) {
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, "banned"));
                            sendCommand(Command.SETPROFILE, null, (String) vector.elementAt(0), attrlist);
                        } else if (inputCommand.equals("ban") && vector.size() >= 1) {
                            String s4 = (String) vector.elementAt(0);
                            int i1 = s4.indexOf('/');
                            if (i1 >= 0) {
                                attrlist.add(new ClientAttr(ClientAttr.BAN_IPSIZE, Integer.parseInt(s4.substring(i1 + 1))));
                                s4 = s4.substring(0, i1);
                            }
                            if (vector.size() > 1) {
                                attrlist.add(new ClientAttr(ClientAttr.BAN_TIMELEFT, getTimeSpanAsMinutesFromText((String) vector1.elementAt(1))));
                            }
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, "banned"));
                            sendCommand(Command.BAN, channel, s4, attrlist);
                        } else if (inputCommand.equals("sban") && vector.size() >= 1) {
                            String s5 = (String) vector.elementAt(0);
                            int j1 = s5.indexOf('/');
                            if (j1 >= 0) {
                                attrlist.add(new ClientAttr(ClientAttr.BAN_IPSIZE, Integer.parseInt(s5.substring(j1 + 1))));
                                s5 = s5.substring(0, j1);
                            }
                            if (vector.size() > 1) {
                                attrlist.add(new ClientAttr(ClientAttr.BAN_TIMELEFT, getTimeSpanAsMinutesFromText((String)vector1.elementAt(1))));
                            }
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, "banned"));
                            sendCommand(Command.BAN, null, s5, attrlist);
                        } else if (inputCommand.equals("unban") && vector.size() >= 1) {
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, "banned"));
                            sendCommand(Command.UNBAN, channel, (String) vector1.elementAt(0), attrlist);
                        } else if (inputCommand.equals("sunban") && vector.size() >= 1) {
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, "banned"));
                            sendCommand(Command.UNBAN, null, (String) vector1.elementAt(0), attrlist);
                        } else if (inputCommand.equals("banlist")) {
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, "banned"));
                            System.out.println("ADZ - Chat.java->input:/banlist:channel="+channel);
                            sendCommand(Command.BANLIST, vector.size() >= 1 ? (String) vector1.elementAt(0) : channel, null, attrlist);
                        } else if (inputCommand.equals("sbanlist")) {
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, "banned"));
                            sendCommand(Command.BANLIST, null, null, attrlist);
                        } else if (inputCommand.equals("register") && vector.size() >= 3) {
                            attrlist.add(new ClientAttr(ClientAttr.NICKNAME, (String) vector.elementAt(0)));
                            attrlist.add(new ClientAttr(ClientAttr.PASSWORD, (String) vector.elementAt(1)));
                            attrlist.add(new ClientAttr(ClientAttr.USERINFO, (String) vector.elementAt(2)));
                            sendCommand(Command.SETACCOUNT, null, myName, attrlist);
                        } else if (inputCommand.equals("sop") && vector.size() >= 1 && getParameter("sop") != null) {
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, vector.size() >= 2 ? (String) vector1.elementAt(1) : getParameter("sop")));
                            sendCommand(Command.SETPROFILE, null, (String) vector.elementAt(0), attrlist);
                        } else if (inputCommand.equals("birthday") && vector.size() >= 1) {
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, "jarig"));
                            sendCommand(Command.SETPROFILE, null, (String) vector.elementAt(0), attrlist);
                        } else if (inputCommand.equals("hide")) {
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, "hide"));
                            sendCommand(Command.SETPROFILE, null, myName, attrlist);
                        } else if (inputCommand.equals("unhide")) {
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, "unhide"));
                            sendCommand(Command.SETPROFILE, null, myName, attrlist);
                        } else if (inputCommand.equals("op") && vector.size() >= 1 && getParameter("op") != null) {
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, vector.size() >= 2 ? (String) vector1.elementAt(1) : getParameter("op")));
                            sendCommand(Command.SETPROFILE, channel, (String) vector.elementAt(0), attrlist);
                        } else if (inputCommand.equals("sdeop") && vector.size() >= 1 && getParameter("sop") != null) {
                            sendCommand(Command.SETPROFILE, null, (String) vector.elementAt(0), attrlist);
                        } else if (inputCommand.equals("deop") && vector.size() >= 1 && getParameter("op") != null) {
                            sendCommand(Command.SETPROFILE, channel, (String) vector.elementAt(0), attrlist);
                        } else if (inputCommand.equals("autosop") && vector.size() >= 1) {
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, vector.size() >= 2 ? (String) vector1.elementAt(1) : getParameter("sop")));
                            sendCommand(Command.BAN, null, (String) vector.elementAt(0), attrlist);
                        } else if (inputCommand.equals("autoop") && vector.size() >= 1) {
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, vector.size() >= 2 ? (String) vector1.elementAt(1) : getParameter("op")));
                            sendCommand(Command.BAN, channel, (String) vector.elementAt(0), attrlist);
                        } else if (inputCommand.equals("autosdeop") && vector.size() >= 1) {
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, vector.size() >= 2 ? (String) vector1.elementAt(1) : getParameter("sop")));
                            sendCommand(Command.UNBAN, null, (String) vector.elementAt(0), attrlist);
                        } else if (inputCommand.equals("autodeop") && vector.size() >= 1) {
                            attrlist.add(new ClientAttr(ClientAttr.PROFILE, vector.size() >= 2 ? (String) vector1.elementAt(1) : getParameter("op")));
                            sendCommand(Command.UNBAN, channel, (String) vector.elementAt(0), attrlist);
                        } else if (inputCommand.equals("autooplist")) {
                            sendCommand(Command.BANLIST, vector.size() >= 1 ? (String) vector1.elementAt(0) : channel, null);
                        } else if (inputCommand.equals("autosoplist")) {
                            sendCommand(Command.BANLIST);
                        } else if (inputCommand.equals("slog")) {
                            attrlist.add(new ClientAttr(ClientAttr.LOG_TIME, nextLogTime));
                            sendCommand(Command.GETLOG, null, null, attrlist);
                        } else if (inputCommand.equals("slogall")) {
                            sendCommand(Command.GETLOG);
                        } else if (inputCommand.startsWith("topic") && getParameter("topic") != null) {
                            attrlist.add(new ClientAttr(ClientAttr.INDEX, inputCommand.length() > 5 ? inputCommand.substring(5) : getParameter("topic")));
                            attrlist.add(new ClientAttr(ClientAttr.CHANNELINFO, vector.size() >= 1 ? (String) vector1.elementAt(0) : ""));
                            sendCommand(Command.SETINFO, channel, null, attrlist);
                        } else if (inputCommand.startsWith("userinfo") && getParameter("info") != null) {
                            attrlist.add(new ClientAttr(ClientAttr.INDEX, inputCommand.length() > 4 ? inputCommand.substring(4) : getParameter("info")));
                            attrlist.add(new ClientAttr(ClientAttr.USERINFO, vector.size() >= 1 ? (String) vector1.elementAt(0) : ""));
                            sendCommand(Command.SETINFO, null, myName, attrlist);
                        } else if (inputCommand.equals("info")) {
                            sendCommand(Command.GETINFO);
                        } else if (inputCommand.equals("enqueue")) {
                            sendCommand(Command.ENQUEUE, channel, vector.size() >= 1 ? (String) vector1.elementAt(0) : null);
                        } else if (inputCommand.equals("dequeue")) {
                            sendCommand(Command.DEQUEUE, channel, vector.size() >= 1 ? (String) vector1.elementAt(0) : null);
                        } else if (inputCommand.equals("autoqueue") && vector.size() >= 1) {
                            attrlist.add(new ClientAttr(ClientAttr.AUTOMAX, Integer.parseInt((String) vector.elementAt(0))));
                            if (vector.size() >= 2) {
                                attrlist.add(new ClientAttr(ClientAttr.AUTOPERIOD, Integer.parseInt((String) vector1.elementAt(1))));
                            }
                            sendCommand(Command.AUTOQUEUE, channel, null, attrlist);
                        } else if (inputCommand.equals("list")) {
                            sendCommand(Command.CHANNELLIST);
                        } else if (inputCommand.equals("clear")) {
                            clearNotes(channel);
                        } else if (inputCommand.equals("help")) {
                            doInternalEvent("help");
                        } /*else if (inputCommand.equals("allowdarkcolours")) {
                            
                            if (vector.size() != 1 || (!vector.elementAt(0).toString().equalsIgnoreCase("true")
                                && !vector.elementAt(0).toString().equalsIgnoreCase("false")))
                            {
                                System.out.println("PROBLEM="+vector.size());
                                try {
                                    AttrList atl = new AttrList();
                                    atl.add(new ClientAttr(ClientAttr.NOTE_TEXT, "Usage: /allowdarkcolours [true or false]"));
                                    atl.add(new ClientAttr(ClientAttr.CHANNELNAME, this.getMyChannel()));
                                    System.out.printf("AttrList="+atl);
                                    doEvent(new Event(Event.NOTE, atl));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                            else
                                this.allowDarkColors = vector.elementAt(0).toString().equalsIgnoreCase("true") ? true : false;
                                
                        }*/ else if (inputCommand.equals("smilies")) {
                            AttrList at = new AttrList();
                            String smileyList = "[ Smilies: ";
                            boolean first = true;
                            for (FilterKey key : smileyFilter.getFilterKeys()) {
                                if (first) {
                                    first = false;
                                } else {
                                    smileyList += " , ";
                                }
                                smileyList += key.getPart2() + " = " + key.getPart1();
                            }
                            smileyList += " ]";
                            at.add(new ClientAttr(ClientAttr.NOTE_TEXT, smileyList));
                            at.add(new ClientAttr(ClientAttr.NOTE_FORMAT, "\033B;"));
                            at.add(new ClientAttr(ClientAttr.VARVALUE, Message.ESC_NOFILTER));
                            doInternalEvent("NOTE", at);
                        } else if (inputCommand.equals("select") && vector.size() >= 1) {
                            selectNick((String) vector1.elementAt(0), null, 0);
                        } else if (inputCommand.equals("deselect")) {
                            deselectNick();
                        } else if (inputCommand.equals(extra)) {
                            selectNick((String) vector1.elementAt(0), null, 0);
                            doInternalEvent(extra);
                        } else if (inputCommand.equals("openprivate") && vector.size() >= 1) {
                            startPrivate((String) vector1.elementAt(0));
                        } else if (inputCommand.equals("closeprivate") && vector.size() >= 1) {
                            endPrivate((String) vector1.elementAt(0));
                        } else if (inputCommand.equals("color")) {
                            sendTextColorCommand(vector.size() >= 1 ? (String) vector1.elementAt(0) : null);
                            /*AttrList at = new AttrList();
                            at.add(new ClientAttr(ClientAttr.TEXTCOLOR, vector.size() >= 1 ? (String) vector1.elementAt(0) : null));
                            sendCommand(new Command(Command.COLORUSE, at));*/
                        } else if (inputCommand.equals("nocolor")) {
                            setTextColor(null);
                        } else if (inputCommand.equals("quit")) {
                            quit();
                        } else if (inputCommand.equals("ignore") && vector.size() >= 1) {
                            setIgnoreNick((String) vector1.elementAt(0), true);
                        } else if (inputCommand.equals("unignore") && vector.size() >= 1) {
                            setIgnoreNick((String) vector1.elementAt(0), false);
                        } else if (inputCommand.equals("ignoreall") && vector.size() >= 1) {
                            setIgnoreNickPattern((String) vector1.elementAt(0), true);
                        } else if (inputCommand.equals("unignoreall") && vector.size() >= 1) {
                            setIgnoreNickPattern((String) vector1.elementAt(0), false);
                        } else if (inputCommand.equals("ignoreprivateall") && vector.size() >= 1) {
                            setIgnorePrivateNickPattern((String) vector1.elementAt(0), true);
                        } else if (inputCommand.equals("unignoreprivateall") && vector.size() >= 1) {
                            setIgnorePrivateNickPattern((String) vector1.elementAt(0), false);
                        } else if (inputCommand.equals("ignorelist")) {
                            Vector vector2 = new Vector();
                            for (Enumeration enumeration = ignoreNicks.elements(); enumeration.hasMoreElements();
                                    attrlist.add(new ClientAttr(ClientAttr.NICKNAME, (String) enumeration.nextElement()))) {
                            }
                            for (Enumeration enumeration1 = ignoreNickPatterns.elements(); enumeration1.hasMoreElements();
                                    attrlist.add(new ClientAttr(ClientAttr.NICKNAME, (String) enumeration1.nextElement()))) {
                            }
                            for (Enumeration enumeration2 = ignorePrivateNickPatterns.elements(); enumeration2.hasMoreElements();
                                    attrlist.add(new ClientAttr(ClientAttr.TARGETNAME, myName))) {
                                attrlist.add(new ClientAttr(ClientAttr.NICKNAME, (String) enumeration2.nextElement()));
                            }

                            doInternalEvent(inputCommand, attrlist);
                        } else if (inputCommand.equals("rehash")) {
                            sendCommand(Command.REHASH);
                        } else if (inputCommand.equals("exec") && vector.size() >= 1) {
                            attrlist.add(new ClientAttr(ClientAttr.EXEC, (String) vector1.elementAt(0)));
                            sendCommand(Command.EXEC, noChannel ? null : channel, noChannel ? targetName : null, attrlist);
                        } else if (inputCommand.equals("raw") && vector.size() >= 1) {
                            sendCommand(new Command(new StringReader((new StringBuilder(String.valueOf((String) vector1.elementAt(0)))).append('\n').toString())));
                        } else if (debug && inputCommand.equals("echo") && vector.size() >= 1) {
                            doEvent(new Event(new StringReader((new StringBuilder(String.valueOf((String) vector1.elementAt(0)))).append('\n').toString())));
                        } else if (inputCommand.equals("version")) {
                            AttrList versionInfo = new AttrList();
                            versionInfo.add(new ClientAttr(ClientAttr.NOTE_TEXT, "\033B;\033I;Version " + Chat.version + "\033i;\033b;"));
                            doEvent(new Event(Event.NOTE, versionInfo));
                        } else {
                            throw new Exception();
                        }
                    }
                } catch (Exception exception1) {
                    AttrList attrlist1 = new AttrList();
                    attrlist1.add(new ClientAttr(ClientAttr.EXEC, text.substring(1)));
                    if (noChannel && targetName != null && targetName.length() > 0 && !targetName.equals("*")) {
                        attrlist1.add(new ClientAttr(ClientAttr.TARGETNAME, targetName));
                    }
                    if (channel != null) {
                        attrlist1.add(new ClientAttr(ClientAttr.CHANNELNAME, channel));
                    }
                    doInternalEvent("failed", attrlist1);

                    throw exception1;
                }
            }
        } catch (Exception exception) {
            if (debug) {
                exception.printStackTrace();
            }
            setStatus(exception);
        }
        return true;
    }

    void sendText(String targetName, String channelName, String noteText, String textCol, AttrList attrlist)
            throws Exception {

        StringBuilder stringbuffer = new StringBuilder();
        try {
            if (textCol != null && textCol.length() == 0) {
                textCol = null;
            }
            if (textCol != null) {
                try {
                    String s4;
                    for (s4 = Integer.toHexString(Color.decode(textCol).getRGB() & 0xffffff); s4.length() < 6; s4 = (new StringBuilder("0")).append(s4).toString()) {
                    }
                    stringbuffer.append((new StringBuilder("\033C#")).append(s4).append(";").toString());
                } catch (Exception e) {
                }
            }
            if (stringbuffer.length() > 0) {
                attrlist.add(new ClientAttr(ClientAttr.NOTE_FORMAT, stringbuffer.toString()));
            }
            if (noteText != null && noteText.length() > 0) {
                attrlist.add(new ClientAttr(ClientAttr.NOTE_TEXT, noteText));
            }
            sendCommand(Command.NOTE, channelName, targetName, attrlist);
        } catch (Exception e) {
            setStatus(e);
        }
    }

    public boolean createChannel(String channel, String channelInfo) {
        return createChannel(channel, channelInfo, null);
    }

    public boolean createChannel(String channel, String channelInfo, String channelType) {
        createChannelName = channel;
        createChannelInfo = channelInfo;
        joinChannel(channel, channelType);
        return true;
    }

    private boolean partLruChannels(int i) {
        String fixChannel = getParameter("fixChannel");
        if (fixChannel != null) {
            fixChannel = fixChannel.toLowerCase();
        }
        try {
            int j = 0;
            for (int k = 0; k < myChannelsLru.size() && (myChannelsLru.size() + i) - j > maxChannels; k++) {
                String channel = (String) myChannelsLru.elementAt(k);
                if (fixChannel == null || !fixChannel.equalsIgnoreCase(channel)) {
                    sendCommand(Command.PART, channel, null);
                    j++;
                }
            }

            return (myChannelsLru.size() + i) - j <= maxChannels;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return false;
    }

    private void activateMruChannel() {
        int i = myChannelsLru.size();
        if (i > 0) {
            myActiveChannel = (String) myChannelsLru.elementAt(i - 1);
            changedActiveChannel();
            sendCommand(Command.NICKLIST, myActiveChannel, null);
        } else {
            myActiveChannel = null;
            changedActiveChannel();
            if (expectFinalPart) {
                doInternalEvent("nochannel");
            }
        }
    }

    public boolean changeChannel() {
        if (myChannelsLru.size() > 0) {
            joinChannel((String) myChannelsLru.elementAt(0), null);
        }
        return true;
    }

    public boolean setMyChannel(String channel) {
        joinChannel(channel, null);
        return true;
    }

    public void joinChannel(String channel, String channelType) {
        if (channel != null && myChannels.containsKey(channel.toLowerCase())) {
            myActiveChannel = (String) myChannels.get(channel.toLowerCase());
            myChannelsLru.removeElement(myActiveChannel);
            myChannelsLru.addElement(myActiveChannel);
            changedActiveChannel();
            sendCommand(Command.NICKLIST, myActiveChannel, null);
            return;
        }
        if (!partLruChannels(myActiveChannel != null ? 1 : 0)) {
            return;
        }
        if (channel == null) {
            activateMruChannel();
            expectFinalPart = myActiveChannel == null;
            return;
        }
        try {
            AttrList attrlist = new AttrList();
            if (channelType != null) {
                attrlist.add(new ClientAttr(ClientAttr.CHANNELTYPE, channelType));
            }
            sendCommand(Command.JOIN, channel, null, attrlist);
            expectJoin = true;
            expectFinalPart = false;
        } catch (Exception exception) {
            setStatus(exception);
        }
    }

    public boolean listChannels() {
        try {
            sendCommand(Command.CHANNELLIST);
        } catch (Exception exception) {
            setStatus(exception);
        }
        return true;
    }

    public boolean listNicks(String channelName) {
        try {
            System.out.println("ADZ - Chat.java->listNick::"+channelName);
            sendCommand(Command.NICKLIST, channelName, null);
        } catch (Exception exception) {
            setStatus(exception);
        }
        return true;
    }

    public void quit() {
        try {
            sendCommand(Command.QUIT);
        } catch (Exception exception) {
            setStatus(exception);
        }
    }

    public void kick(String channelName, String targetName) {
        try {
            sendCommand(Command.KICK, channelName, targetName);
        } catch (Exception exception) {
            setStatus(exception);
        }
    }

    public void ban(String channelName, String targetName) {
        try {
            sendCommand(Command.BAN, channelName, targetName);
        } catch (Exception exception) {
            setStatus(exception);
        }
    }

    public void unban(String channelName, String targetName) {
        try {
            sendCommand(Command.UNBAN, channelName, targetName);
        } catch (Exception exception) {
            setStatus(exception);
        }
    }

    public void setUserProfile(String channelName, String targetName, String profile) {
        try {
            AttrList attrlist = new AttrList();
            if (profile != null && profile.length() > 0) {
                attrlist.add(new ClientAttr(ClientAttr.PROFILE, profile));
            }
            sendCommand(Command.SETPROFILE, channelName, targetName, attrlist);
        } catch (Exception exception) {
            setStatus(exception);
        }
    }

    public void onevent(String s) {
        onevent(s, new AttrList());
    }

    private void delayOnEvent(String s) {
        if (getParameter(s) != null);
    }

    private void onevent(String s, AttrList attrlist) {
        System.out.println("ADZ - Chat.java->onEvent::"+s+"::"+attrlist);
        try {
            String s1 = getParameter((new StringBuilder("on")).append(s).toString());
            System.out.println("ADZ - Chat.java->onEvent:s1="+s1);
            if (s1 != null) {

                long l;
                long l1;

                while ((l1 = (frameLastTime + frameDelay) - (l = System.currentTimeMillis())) > 0L) {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException interruptedexception) {
                    }
                }

                frameLastTime = l;

                StringBuffer stringbuffer = new StringBuffer();
                StringBuffer stringbuffer1 = new StringBuffer();
                Message.expand(s1, attrlist, new StringBuffer[]{
                            stringbuffer, stringbuffer1
                        }, 0, getFilters());
                System.out.println("ADZ - Chat.java->onEvent:stringbuffer.toString="+stringbuffer.toString());
                if (stringbuffer.length() > 0) {
                    boolean flag = false;
                    URL url = new URL(getDocumentBase(), stringbuffer.toString());
                    String urlHost = url.getHost().toLowerCase();
                    System.out.println("ADZ - Chat.java->onEvent:urlHost="+urlHost);
                    if (allowedHosts == null) {
                        flag = true;
                    } else if (urlHost.equalsIgnoreCase(getCodeBase().getHost())) {
                        flag = true;
                    } else {
                        for (Enumeration enumeration = allowedHosts.elements(); enumeration.hasMoreElements();) {
                            if (urlHost.endsWith(((String) enumeration.nextElement()).toLowerCase())) {
                                flag = true;
                                break;
                            }
                        }

                    }
                    if (!flag) {
                        if (debug) {
                            System.err.println((new StringBuilder("onevent(")).append(s).append("): URL '").append(url).append("' for frame '").append(stringbuffer1.toString()).append("' blocked").toString());
                        }
                    } else {
                        if (debug) {
                            System.err.println((new StringBuilder("onevent(")).append(s).append("): URL '").append(url).append("' for frame '").append(stringbuffer1.toString()).append("' accepted").toString());
                        }
                        
                        System.out.println("ADZ - Chat.java->onEvent:url="+url);

                        if (s.equalsIgnoreCase("passwordlogin"))
                             sendChallengeePasswordResponse(getChallengeePassword());
                        else
                            getAppletContext().showDocument(url, stringbuffer1.toString());
                    }
                }
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            setStatus(throwable);
        }
    }

    public String expandEvent(String eventName, String s1) {
        AttrList attrlist = (AttrList) evtLastParms.get(eventName);
        StringBuffer stringbuffer = new StringBuffer();
        if (attrlist == null) {
            return null;
        } else {
            Message.expand(s1, attrlist, new StringBuffer[]{
                        stringbuffer
                    }, 1, getFilters());
            return stringbuffer.toString();
        }
    }

    private void fixEventParams(AttrList attrlist)
            throws ChatException {
        System.out.println("ADZ - Chat.java->fixEventParams::"+attrlist);
        ClientAttr clientattr = (ClientAttr) attrlist.get(ClientAttr.ACTORPROFILE);
        if (ignoreColor) {
            attrlist.remove(ClientAttr.NOTE_FORMAT);
            attrlist.remove(ClientAttr.NOTE_FORMATCLOSE);
        }
        if (clientattr != null) {
            String s = clientattr.getString();
            System.out.println("ADZ - Chat.java->fixEventParams:clientattr.getString()=:"+clientattr.getString());
            String s1 = getParameter((new StringBuilder("format")).append(s).toString());
            System.out.println("ADZ - Chat.java->fixEventParams:s1=:"+s1);
            if (s1 != null) {
                StringBuffer stringbuffer = new StringBuffer();
                Message.expand(s1, new AttrList(), new StringBuffer[]{
                            stringbuffer
                        }, 0, new Filter[0]);
                s1 = stringbuffer.toString();
                ClientAttr clientattr1 = (ClientAttr) attrlist.get(ClientAttr.NOTE_FORMAT);
                if (clientattr1 != null) {
                    s1 = (new StringBuilder(String.valueOf(s1))).append(clientattr1.getString()).toString();
                }
                attrlist.set(new ClientAttr(ClientAttr.NOTE_FORMAT, s1));
            }
            String s2 = getParameter((new StringBuilder("formatclose")).append(s).toString());
            System.out.println("ADZ - Chat.java->fixEventParams:s2=:"+s2);
            if (s2 != null) {
                StringBuffer stringbuffer1 = new StringBuffer();
                Message.expand(s2, new AttrList(), new StringBuffer[]{
                            stringbuffer1
                        }, 0, new Filter[0]);
                s2 = stringbuffer1.toString();
                System.out.println("ADZ - Chat.java->fixEventParams:s2 after message expand=:"+s2);
                ClientAttr clientattr2 = (ClientAttr) attrlist.get(ClientAttr.NOTE_FORMATCLOSE);
                if (clientattr2 != null) {
                    s2 = (new StringBuilder(String.valueOf(clientattr2.getString()))).append(s2).toString();
                }
                attrlist.set(new ClientAttr(ClientAttr.NOTE_FORMATCLOSE, s2));
            }
        }
    }

    public int getTimeSpanAsMinutesFromText(String text) {
        
        text = text.toLowerCase();
        
        int mins = 0;

        int pos = 0;
        int start = 0;

        while (pos < text.length()) {

            while (Character.isDigit(text.charAt(pos))) {
                pos++;
            }

            if (pos - start != 0) {
                Character type = text.charAt(pos);
                int number = Integer.valueOf(text.substring(start, pos-1));
                if (type == 'd') {
                    mins += number*24*60;
                } else if (type == 'h') {
                    mins += number*60;
                } else if (type == 'm') {
                    mins += number;
                }
            }

            pos++;
            start = pos;
        }

        return mins;
    }
    private boolean loginPwWasWrong = false;
    private String loginPwDigestSalt = "";

    public boolean loginPwWasWrong() {
        return loginPwWasWrong;
    }
    
    
    public String getTmpLoginName()
    {
        return tmpLoginName;
    }
    
    public boolean allowDarkColors()
    {
        return this.allowDarkColors;
    }
    
    public void setAllowDakColors(boolean value)
    {
        this.allowDarkColors = value;
    }
    
    private String status;
    private Color clrError;
    private Color clrCommand;
    private Color clrEvent;
    private Connection cxn;
    private Idler idler;
    private Vector listeners;
    private Vector newListeners;
    private int listenersLocks;
    private String myName;
    private AttrList myUserInfo;
    private Perms myPerms;
    private String myProfile;
    private String myActiveChannel;
    private Hashtable myChannels;
    private Vector myChannelsLru;
    private Hashtable myChannelsInfo;
    private Hashtable myChannelsPerms;
    private Hashtable myChannelsProfile;
    private String selectedNick;
    private int useAWTEventQueue;
    private int maxChannels;
    private int maxHistory;
    private int maxClientHistory;
    private Vector allowedHosts;
    private Filter filter;
    private Filter smileyFilter;
    private Hashtable actionCmds;
    private String extra;
    private long frameDelay;
    private long frameLastTime;
    static boolean debug = true;
    private String quotes;
    private ConcurrentLinkedQueue<Event> pendingEvents;
    private Vector evtHistory;
    private Hashtable evtLastParms;
    private int nextLogTime;
    private String loginInfo;
    private String createChannelName;
    private String createChannelInfo;
    private boolean expectJoin;
    private boolean expectFinalPart;
    private Hashtable ignoreNicks;
    private Vector ignoreNickPatterns;
    private Vector ignorePrivateNickPatterns;
    private boolean ignoreEvents[];
    private boolean ignoreColor;
    private boolean ignorePrivate;
    private boolean ignorePicture;
    private String textColor;
    private boolean doPrivateWindows;
    private static Hashtable chats = new Hashtable();
    private Hashtable histories;
    private Hashtable states;
    private boolean hidden;
    private String tmpLoginName;
    private boolean allowDarkColors;
    private String lastCommand = "";
    private boolean blockCommand = false;
    private int commandReuseCount = 0;
}
